strip-dirs
Remove leading directory components from a path, like tar(1)'s --strip-components
option
var stripDirs = require('strip-dirs');
stripDirs('foo/bar/baz', 1);
stripDirs('foo/bar/baz', 2);
stripDirs('foo/bar/baz', 999);
Installation
Use npm.
npm install --save strip-dirs
API
var stripDirs = require('strip-dirs');
stripDirs(path, count [, option])
path: String
(A relative path)
count: Number
(0, 1, 2, ...)
option: Object
Return: String
It removes directory components from the beginning of the path by count.
var stripDirs = require('strip-dirs');
stripDirs('foo/bar', 1);
stripDirs('foo/bar/baz', 2);
stripDirs('foo/././/bar/./', 1);
stripDirs('foo/bar', 0);
stripDirs('/foo/bar', 1)
If you want to remove all directory components certainly, use path.basename
instead of this module.
option.narrow
Type: Boolean
Default: false
By default, it keeps the last path component when path components are fewer than the count.
If this option is enabled, it throws an error in such case.
stripDirs('foo/bar/baz', 9999);
stripDirs('foo/bar/baz', 9999, {narrow: true});
CLI
You can use this module as strip-dirs
command by installing it globally.
npm install -g strip-dirs
Usage
strip-dirs <string> --count(or -c) <number> [--narrow(or -n)]
Or, use with pipe(|
):
echo <string> | strip-dirs --count(or -c) <number> [--narrow(or -n)]
Flags
--count, -c: Number of directories to strip from the path
--narrow, -n: Disallow surplus count of directory level
License
Copyright (c) Shinnosuke Watanabe
Licensed under the MIT License.